package com.guigui.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.DesignProcedureDto;
import com.guigu.erp.dto.ManufactureDesignProcedureDetailsDto;
import com.guigu.erp.dto.ManufactureDesignProcedureDto;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import com.guigui.erp.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Lenovo
 */
@Controller
@RequestMapping("/design/procedure")
public class DesignProcedureController {
    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;

    @Autowired
    private DesignProcedureService designProcedureService;

    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;

    /**
     *查询制定物料设计单页面
     */
    @RequestMapping("/DesignProcedurePage")
    @ResponseBody
    public PageUtils<DesignProcedure> DesignProcedurePage(int pageNo, int pageSize) {
        PageUtils<DesignProcedure> page = this.designProcedureService.DesignProcedurePage(pageNo, pageSize);
        return page;
    }

    /**
     * 处理制定物料设计单的确认按钮请求
     */
    @RequestMapping("/add")
    @ResponseBody
    public String addProcedure(@RequestBody DesignProcedureDto designProcedureDto) {

        // 生成物料设计单编号
        String designId = numberUtils.getTypeNumber("201");
        designProcedureDto.setDesignId(designId);

        // 设置物料总成本
        BigDecimal sum = new BigDecimal(0);
        List<ManufactureDesignProcedureDetails> details = designProcedureDto.getDetails();
        System.out.println("details = " + details);
        for (int i = 0; i < details.size(); i++) {
            System.out.println("sum = " + sum);
            sum=sum.add(details.get(i).getSubtotal());
        }
        designProcedureDto.setCostPriceSum(sum);

        // 设置审核标志 S001-0 等待审核
        designProcedureDto.setCheckTag("S001-0");

        // 设置变更标志 B002-0 未变更
        designProcedureDto.setChangeTag("B002-0");

        //工序物料设计标志 G002-0 未设计
        designProcedureDto.setDesignModuleTag("G002-0");

        //工序物料变更标志 G003-0 未变更
        designProcedureDto.setDesignModuleChangeTag("G003-0");

        //设置当前物料设计单的分类编号
        String productId = designProcedureDto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<DesignFile>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        if (designFile != null) {
            designProcedureDto.setFirstKindId(designFile.getFirstKindId());
            designProcedureDto.setFirstKindName(designFile.getFirstKindName());
            designProcedureDto.setSecondKindId(designFile.getSecondKindId());
            designProcedureDto.setSecondKindName(designFile.getSecondKindName());
            designProcedureDto.setThirdKindId(designFile.getThirdKindId());
            designProcedureDto.setThirdKindName(designFile.getThirdKindName());
        }
        //调用业务层方法
        manufactureDesignProcedureService.makeDesignProcedure(designProcedureDto);
        return "correct";
    }

    /**
     * 生产工序设计单的复核查询
     */
    @RequestMapping("/designProcedure")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureDesignProcedure> recheckPage = this.manufactureDesignProcedureService.recheckPage(vo, pageNo, pageSize);
        return recheckPage;
    }

    /**
     * 根据生产工序设计单id查询对应的生产工序设计单信息
     */
    @RequestMapping("/designProcedureGetId")
    @ResponseBody
    public ManufactureDesignProcedure designProcedureGetId(int id) {
        ManufactureDesignProcedure manufactureDesignProcedure = this.manufactureDesignProcedureService.getById(id);
        return manufactureDesignProcedure;
    }

    /**
     * 生产工序设计复核通过
     * @param
     * @return
     */
    @RequestMapping("designProcedureReviewPassed")
    @ResponseBody
    public String designProcedureReviewPassed(ManufactureDesignProcedure manufactureDesignProcedure) {
        manufactureDesignProcedure.setCheckTag("S001-1");
        boolean flag = manufactureDesignProcedureService.updateById(manufactureDesignProcedure);
        if (flag) {
            return "correct";
        }
        return "error";
    }

    /**
     * 物料组成设计查询请求
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> procedureList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureDesignProcedure> page = this.manufactureDesignProcedureService.procedureListPage(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 按条件分页查询物料设计单未设计的
     */
    @RequestMapping("/moduleRecheckPage")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> moduleRecheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureDesignProcedure> recheckPage = this.manufactureDesignProcedureService.moduleRecheckPage(vo, pageNo, pageSize);
        return recheckPage;
    }

    /**
     * 根据父级编号查询工序明细*/
    @RequestMapping("/detailsList")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedureDetails> details(Integer parentId,int pageSize,int pageNo){
        return manufactureDesignProcedureDetailsService.detail(parentId,pageNo,pageSize);
    }

    /**
     * 根据产品编号查询物料明细*/
    @RequestMapping("/getModuleById")
    @ResponseBody
    public PageUtils<DesignModuleDetails> getModule(String productId,int pageSize,int pageNo){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("product_id",productId);
        List<DesignModule> list = designModuleService.list(wrapper);
        DesignModule designModule=null;
        for (int i=0;i<list.size();i++){
            designModule = list.get(i);
        }
        return designModuleDetailsService.moduledetailsPage(designModule.getId(),pageNo,pageSize);
    }

    /**
     * 添加工序物料和明细
     * @param manufactureDesignProcedureDetailsDto
     * @return
     */
    @RequestMapping("/addModule")
    @ResponseBody
    //添加工序物料
    public String addModule(@RequestBody ManufactureDesignProcedureDetailsDto manufactureDesignProcedureDetailsDto){
        //设置物料设计标志 D002-1
        manufactureDesignProcedureDetailsDto.setDesignModuleTag("D002-1");
        //调用业务层
        manufactureDesignProcedureDetailsService.makeModule(manufactureDesignProcedureDetailsDto);
        return "correct";
    }

    /**
     * 清除物料明细
     * @param id
     * @return
     */
    @RequestMapping("/delModule")
    @ResponseBody
    public String delModule(@RequestBody  int id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        manufactureDesignProcedureModuleService.remove(wrapper);
        return "correct";
    }

    /**
     * 修改工序明细*/
    @RequestMapping("upd_procedure_detail")
    @ResponseBody
    public String upd_procedure_detail(@RequestBody List<ManufactureDesignProcedureDetails> list){
        for (int i=0;i<list.size();i++){
            ManufactureDesignProcedureDetails procedureDetails = list.get(i);
            procedureDetails.setDesignModuleTag("D002-1");
            manufactureDesignProcedureDetailsService.updateById(procedureDetails);
        }
        return "correct";
    }

    /**
     * 产品生产工序明细查询通过设计编号查询
     */
    @RequestMapping("/listByIdDetails")
    @ResponseBody
    public List<ManufactureDesignProcedureDetails> prodecureListById(int id){
        List<ManufactureDesignProcedureDetails> listById=
                manufactureDesignProcedureDetailsService.detailsById(id);
        return listById;
    }

    /**
     * 修改产品工序工序物料标志
     * @param productId
     * @return
     */
    @RequestMapping("/upd_module_tag")
    @ResponseBody
    public String upd_module_tag(String productId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("product_id",productId);
        ManufactureDesignProcedure procedure=new ManufactureDesignProcedure();
        procedure.setDesignModuleTag("G002-1");
        manufactureDesignProcedureService.update(procedure,wrapper);
        return "correct";
    }

    /**
     * 物料组成设计查询请求
     */
    @RequestMapping("/list2")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> procedureList2(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureDesignProcedure> page = this.manufactureDesignProcedureService.procedureListPage2(vo, pageNo, pageSize);
        return page;
    }
}
